package cn.bkrc.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RedisUtils {

    //JDK序列化<String,(Object)String>
    // StringRedisTemplate序列化
    // StringRedisTemplate extents RedisTemplate<String,String>
    @Autowired
    private StringRedisTemplate redisTemplate;

    public String get(final String key){
        return redisTemplate.opsForValue().get(key);
    }

    public  boolean set(final String key,String value){

        boolean result=false;
        try {
        redisTemplate.opsForValue().set(key,value);
        result=true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public boolean getAndSet(final String key,String value){
        boolean result=false;

        try {
            redisTemplate.opsForValue().getAndSet(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public  boolean delete(final String key){
        boolean result=false;
        try {
            redisTemplate.delete(key);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }
}
